package vip.zhenzicheng.design_patterns.singleton.lazy;

/**
 * 使用静态内部类实现懒汉
 *
 * @author zhenzicheng
 * @date: 2022-05-24 23:27
 */
public class SingletonWithStaticClass {
  private static SingletonWithStaticClass INSTANCE;

  private SingletonWithStaticClass() {
  }

  public static SingletonWithStaticClass getInstance() {
    return SingletonHolder.INSTANCE;
  }

  /**
   * 静态内部类在JVM加载时第一次不会加载，只有用到的时候才会加载。并且static的类JVM保证只初始化一次，不存在线程不安全问题。
   */
  private static class SingletonHolder {
    private static final SingletonWithStaticClass INSTANCE = new SingletonWithStaticClass();

  }

}
